package com.example.ywj.toutiao.utils;

import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.ywj.toutiao.R;
import com.example.ywj.toutiao.bean.Comment;
import com.example.ywj.toutiao.bean.Item;

import java.util.List;

public class MyCommentBaseAdapter extends BaseAdapter {
    private List<Comment> list;
    public MyCommentBaseAdapter(List<Comment> list){
        this.list = list;

    }
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //int currentType = getItemViewType(position);
        ViewHolder viewHolder ;
        if(convertView == null){
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            convertView = inflater.inflate(R.layout.comment_item,null);
            viewHolder = new ViewHolder();
            viewHolder.iv = convertView.findViewById(R.id.comment_list_avatar);
            viewHolder.tv_nickname = convertView.findViewById(R.id.comment_list_nickname);
            viewHolder.tv_content = convertView.findViewById(R.id.comment_list_content);
            viewHolder.tv_time = convertView.findViewById(R.id.comment_list_time);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(R.drawable.placeholder).error(R.drawable.error_img);
        String url = Constant.BASEURL+"public/images/"+list.get(position).getAvatar()+".jpg";
        Glide.with(parent.getContext()).load(url).apply(requestOptions).into(viewHolder.iv);
        viewHolder.tv_nickname.setText(list.get(position).getNickname());
        viewHolder.tv_content.setText(list.get(position).getContent());
        viewHolder.tv_time.setText(list.get(position).getTime());
        return convertView;
    }
    class ViewHolder{
        ImageView iv;
        TextView tv_nickname;
        TextView tv_content;
        TextView tv_time;
    }
}
